from flask import Flask, jsonify
from flask import render_template
import utils

app = Flask(__name__)

@app.route('/c1')
def get_c1_data():
    return jsonify(utils.get_c1_data())

@app.route('/')
def hello_world():
    return render_template('main.html')

@app.route('/c2')
def get_c2_data():
    res = []
    for tup in utils.get_c2_data():
        res.append({"name":tup[0],"value":int(tup[1])})
    return jsonify({"data":res})

@app.route('/r1')
def get_r1_data():
    data = utils.get_r1_data()
    province = []
    confirm = []
    for k,v in data:
        province.append(k)
        confirm.append(int(v))
    # print(province)
    return jsonify({"province": province,"confirm": confirm})

@app.route('/r2')
def get_r2_data():
    data = utils.get_r2_data()
    day,confirm_add,suspect_add = [],[],[]
    for a,b,c in data[7:]:
        day.append(a.strftime("%m-%d"))
        confirm_add.append(b)
        suspect_add.append(c)
    return jsonify({"day":day,"confirm_add":confirm_add,"suspect_add":suspect_add})

@app.route('/time')
def get_time():
    return utils.get_time()

@app.route('/l1')
def get_l1_data():
    data = utils.get_l1_data()
    day,confirm,suspect,heal,dead = [],[],[],[],[]
    for a,b,c,d,e in data[7:]:
        day.append(a.strftime("%m-%d"))
        confirm.append(b)
        suspect.append(c)
        heal.append(d)
        dead.append(e)
    return jsonify({"day":day,"confirm":confirm,"suspect":suspect,"heal":heal,"dead":dead})

@app.route('/l2')
def get_l2_data():
    data = utils.get_l2_data()
    day,confirm,recover,death=[],[],[],[]

    day = [i for i in range(len(data[0]))]
    for a in data[0]:
        confirm.append(a)
    for a in data[1]:
        recover.append(a)
    for a in data[2]:
        death.append(a)
    # print(len(day))
    # print(len(confirm))
    recover=recover[:-128]
    return jsonify({"day":day,"confirm":confirm,"recover":recover,"death":death})

if __name__ == '__main__':
    app.run()
